<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Your Settings</h1>

          <form>
            <fieldset>
              <fieldset class="form-group">
                <input
                  :disabled="isUpdating"
                  class="form-control"
                  v-model="userForm.image"
                  type="text"
                  placeholder="URL of profile picture"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  :disabled="isUpdating"
                  class="form-control form-control-lg"
                  v-model="userForm.username"
                  required
                  type="text"
                  placeholder="Your Name"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  :disabled="isUpdating"
                  class="form-control form-control-lg"
                  rows="8"
                  v-model="userForm.bio"
                  placeholder="Short bio about you"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  :disabled="isUpdating"
                  class="form-control form-control-lg"
                  v-model="userForm.email"
                  required
                  type="text"
                  placeholder="Email"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  :disabled="isUpdating"
                  class="form-control form-control-lg"
                  v-model="userForm.password"
                  type="password"
                  placeholder="Password"
                />
              </fieldset>
              <button
                :disabled="isUpdating"
                class="btn btn-lg btn-primary pull-xs-right"
                @click="update"
              >Update Settings</button>
            </fieldset>
          </form>

          <hr />
          <button class="btn btn-outline-danger" @click="logout">Or click here to logout.</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { updateUser } from '@/api/user.js'
export default {
  name: 'Settings',
  middleware: "authenticated",
  computed: {
    ...mapState(['user']),
  },

  data() {
    return {
      userForm: {
        email: '',
        username: '',
        password: '',
        image: '',
        bio: '',
      },
      isUpdating: false,
    }
  },

  created() {
    this.userForm = { ...this.userForm, ...this.user }
  },

  methods: {
    async update() {
      this.isUpdating = true
      const { data } = await updateUser(this.userForm)
      const { user } = data
      if (user) {
        this.$store.commit('setUser', user)
      }
      this.isUpdating = false
    },
    logout() {
      this.$store.commit('setUser', null)
      this.$router.push({
        name: 'login'
      })
    },
  },
}
</script>